﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using NLogManager;
using WJ.DataSender.Service.Models.ConfigModel;

namespace WJ.DataSender.Service.Commen
{
    public class JsonFileHelper<T> where T:IConfigModel
    {
        private static readonly IConfiguration _configuration;
        static JsonFileHelper()
        {
            string appFileName = ConstantValues.AppsettingFileName;
            string equipmentFileName = ConstantValues.EquipmentFileName;
            string basePath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/");
            LogHelper.Debug($"配置文件所在目录{basePath}");
            var appFileFullName = $@"{basePath}/{appFileName}";
            var equimentFileFullName = $@"{basePath}/{equipmentFileName}";
            if(!File.Exists(appFileFullName) || !File.Exists(equimentFileFullName))
            {
                throw new FileNotFoundException();
            }
            var builder = new ConfigurationBuilder()
                .AddJsonFile(appFileFullName, false, true)
                .AddJsonFile(equimentFileFullName, false, true);
            _configuration = builder.Build();
        }


        public static T GetConfigInfo()
        {
            T config = default(T);
            try
            {
                config = _configuration.Get<T>();
                return config;
            }
            catch (Exception ex)
            {
                throw ex; 
            }
        }
    }
}
